package cn.cangli.service;

import cn.cangli.mapper.ClazzMapper;
import cn.cangli.mapper.StudentMapper;
import cn.cangli.pojo.Clazz;
import cn.cangli.pojo.ClazzQuery;
import cn.cangli.pojo.Student;
import cn.cangli.service.impl.ClazzServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzService implements ClazzServiceImpl {
    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Clazz> list(ClazzQuery clazzQuery) {
        List<Clazz> list = clazzMapper.list(clazzQuery);
        LocalDate now = LocalDate.now();
        list.forEach(item->{
            if(now.isBefore(item.getBeginDate())) {
                item.setStatus("未开班");
            }
            else if(now.isAfter(item.getEndDate())) {
                item.setStatus("已结课");
            }
            else {
                item.setStatus("在读");
            }
        });
        return list;
    }

    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);

        studentMapper.updateByClazzId(id);
    }

}
